package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 齐鲁热线  网站解析
 * @author Chase
 *
 */
public class SdinfoNewsAnalyse implements AnalyseNews {
	private static final String[] regex={
		"http://.*.sdinfo.net/.*/[0-9]*_[0-9]*.shtml",
		"http://.*.sdinfo.net/.*/[0-9]*.shtml",
		"http://[a-z]+.sdinfo.net/[a-z]+/[0-9]+shtml"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select("div.right").remove();
		doc.select(".xiangguan").remove();
		doc.select(".gengduo").remove();
		/*
		 * 标题
		 */
		title=doc.select("h6").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h4").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h2").text();
			}
		}
		if(StringUtils.isBlank(title)){
		}

		/*
		 * 内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#tbcontent"));

		/*
		 * 内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		if(StringUtils.isBlank(content)){
			 Elements select=doc.select("div#main_content");
			 if(select!=null){
				 select.select("li.xz").remove();
				 content=HtmlCleaner.getContentHtml(url,select);
			 }
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.title").text();
		author=StringUtils.substringBetween(authorStr, "来源："," ");
		if(StringUtils.isBlank(author)){
			author=StringUtils.substringAfter(authorStr, "来源：");
			if(StringUtils.isBlank(author)){
				author=authorStr;
			}
		}

		/*
		 * 时间
		 */
		date=DateUtils.matchDate(doc.select("span").text());

		/*
		 * 链接
		 */
		news.setUrl(url);

		/*
		 * 来源
		 */
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}
	public static void main(String[] args) {
		SdinfoNewsAnalyse analyse=new SdinfoNewsAnalyse();
		String url="http://news.sdinfo.net/shxw/2479765.shtml";
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
